package com.example.warehouse.controller;


import com.example.warehouse.dto.InventoryReportDTO;
import com.example.warehouse.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/inventory")
public class InventoryController {
    @Autowired
    private InventoryService inventoryService;

    // 根据商品ID获取库存数量
    @GetMapping("/{productId}")
    public Integer getInventory(@PathVariable Long productId) {
        // 调用InventoryService获取商品库存
        Integer inventory = inventoryService.getInventoryByProductId(productId);

        // 如果查询结果为 null，表示没有库存记录
        if (inventory == null) {
            return 0; // 或者根据实际需求返回其他值
        }

        return inventory;
    }
    // 根据商品ID检查库存预警
    @GetMapping("/warning/{productId}")
    public String checkInventoryWarning(@PathVariable Long productId) {
        return inventoryService.checkInventoryWarnings(productId);  // 返回库存预警信息
    }


    @GetMapping("/report/daily")
    public String getDailyReport(Model model, @RequestParam LocalDate date) {
        List<InventoryReportDTO> report = inventoryService.generateDailyReport(date);
        model.addAttribute("report", report);
        return "daily_report";
    }

    @GetMapping("/report/monthly")
    public String getMonthlyReport(Model model, @RequestParam LocalDate startDate, @RequestParam LocalDate endDate) {
        List<InventoryReportDTO> report = inventoryService.generateMonthlyReport(startDate, endDate);
        model.addAttribute("report", report);
        return "monthly_report";
    }

    @GetMapping("/report/yearly")
    public String getAnnualReport(Model model, @RequestParam LocalDate startDate, @RequestParam LocalDate endDate) {
        List<InventoryReportDTO> report = inventoryService.generateAnnualReport(startDate, endDate);
        model.addAttribute("report", report);
        return "annual_report";
    }

}
